<?php

namespace Biz;

use Base\Exception\NoRecordException;

class Region extends \Base\BaseBiz
{
    /**
     * 获取地区列表
     * @param $parent_id
     * @return mixed|null
     */
    public function getRegionListByParentId($parent_id)
    {
        $region_orm = new \Orm\Region\RegionList();
        $where = sprintf('Parentid=%d', $parent_id);
        $region_orm['where'] = $where;
        $region_orm['limit'] = \Util\Config::get('max_limit', 'Common');
        $region_orm->load();
        return $region_orm['lists'];
    }

    /**
     * 根据id获取省市区名称
     */
    public function getById($id){
        $region_orm = new \Orm\Region\RegionClass();
        try {
            $region_orm->loadById($id);
        } catch (NoRecordException $e) {
            return $region_orm;
        }
        return $region_orm;
    }

    /**
     * 根据省市区id获取省市区名称
     * @param $ids
     * @return array
     */
    public function getAddrByIds($ids){
        $region_orm = new \Orm\Region\RegionList();
        $ids_array_deal = array();
        foreach ($ids as $value){
            if($value == 0){
                continue;
            }
            $ids_array_deal[] = $value;
        }
        $region_list = array();
        if(count($ids_array_deal)>0){
            $region_orm['where'] = sprintf('id in (%s)',implode(',',$ids_array_deal));
            $region_orm->load();

            if($region_orm->offsetGet('total')>0){
                foreach($region_orm->offsetGet('lists') as $v){
                    $region_list[$v['id']] = $v;
                }
            }
        }
        return $region_list;
    }
}